{% extends "base.html" %}

{% block content %}
<div class="container mt-5">
    <h2 class="mb-4">论文管理</h2>
    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>用户信息</th>
                    <th>预算</th>
                    <th>联系方式</th>
                    <th>状态</th>
                    <th>提交时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for thesis in theses %}
                <tr>
                    <td>{{ thesis.id }}</td>
                    <td>
                        <div>{{ thesis.title }}</div>
                        <small class="text-muted">{{ thesis.programming_language }}</small>
                    </td>
                    <td>
                        <div>{{ thesis.user.username }}</div>
                        <small class="text-muted">{{ thesis.user.email }}</small>
                        {% if thesis.user.last_login %}
                        <br>
                        <small class="text-muted">最后登录: {{ thesis.user.last_login.strftime('%Y-%m-%d') }}</small>
                        {% endif %}
                    </td>
                    <td>
                        <div>¥{{ "%.2f"|format(thesis.price) }}</div>
                        {% if thesis.need_environment %}
                        <small class="text-success">含环境配置</small>
                        {% endif %}
                    </td>
                    <td>
                        <div>{{ thesis.contact_info }}</div>
                        <small class="text-muted">
                            期限: {{ thesis.deadline.strftime('%Y-%m-%d') }}
                        </small>
                    </td>
                    <td>
                        <form action="{{ url_for('admin.update_thesis_status', id=thesis.id) }}" 
                              method="POST" 
                              class="status-form"
                              style="display: inline-block;">
                            <select name="status" 
                                    class="form-select form-select-sm status-select" 
                                    data-thesis-id="{{ thesis.id }}"
                                    style="width: auto;"
                                    onchange="this.form.submit()">
                                <option value="pending" {% if thesis.status == 'pending' %}selected{% endif %}>待处理</option>
                                <option value="accepted" {% if thesis.status == 'accepted' %}selected{% endif %}>已接受</option>
                                <option value="completed" {% if thesis.status == 'completed' %}selected{% endif %}>已完成</option>
                            </select>
                        </form>
                    </td>
                    <td>
                        <div>{{ thesis.created_at.strftime('%Y-%m-%d') }}</div>
                        <small class="text-muted">{{ thesis.created_at.strftime('%H:%M') }}</small>
                    </td>
                    <td>
                        <div class="btn-group">
                            <a href="{{ url_for('main.thesis_detail', id=thesis.id, source='admin') }}" 
                               class="btn btn-info btn-sm" 
                               title="查看详情">
                                <i class="fas fa-eye"></i>
                            </a>
                            <a href="{{ url_for('admin.edit_thesis', id=thesis.id) }}" 
                               class="btn btn-warning btn-sm"
                               title="编辑论文">
                                <i class="fas fa-edit"></i>
                            </a>
                            <button type="button" 
                                    class="btn btn-danger btn-sm" 
                                    onclick="deleteThesis({{ thesis.id }})"
                                    title="删除论文">
                                <i class="fas fa-trash"></i>
                            </button>
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    
    {% if pagination and pagination.pages > 1 %}
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            {% for page in pagination.iter_pages() %}
                {% if page %}
                    <li class="page-item {% if page == pagination.page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('admin.theses', page=page) }}">{{ page }}</a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">...</span>
                    </li>
                {% endif %}
            {% endfor %}
        </ul>
    </nav>
    {% endif %}
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                确定要删除此论文吗？
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="POST" class="d-inline">
                    <button type="submit" class="btn btn-danger">确定删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

{% block scripts %}
<script>
function deleteThesis(id) {
    const deleteForm = document.getElementById('deleteForm');
    deleteForm.action = "{{ url_for('admin.delete_thesis', id=0) }}".replace('0', id);
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}

document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.status-select').forEach(select => {
        select.addEventListener('change', function() {
            this.closest('form').submit();
        });
    });
});
</script>
{% endblock %}
{% endblock %} 